//
//  TWSTabBarController.m
//  TWSWeibo
//
//  Created by Twisal on 2016/12/27.
//  Copyright © 2016年 Twisal. All rights reserved.
//

#import "TWSTabBarController.h"
#import "TWSNavigationController.h"
#import "TWSBaseViewController.h"
#import "TWSComposeViewController.h"


@interface TWSTabBarController ()
@property (nonatomic, strong) UIButton *composeBtn;
@end

@implementation TWSTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setupTabBarItems];
    [self setupViewControllers];
    [self setupComposeButton];
}

#pragma mark - 初始化操作
/**
 初始化tabBarItem
 */
- (void)setupTabBarItems {
    UITabBarItem *item = [UITabBarItem appearance];
    NSMutableDictionary *attriDict = [NSMutableDictionary dictionary];
    attriDict[NSForegroundColorAttributeName] = [UIColor grayColor];
    attriDict[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:attriDict forState:UIControlStateNormal];
    
    // 设置文字属性(UIControlStateSelected)
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}


/**
 初始化button
 */
- (void)setupComposeButton {
    [self.tabBar addSubview:self.composeBtn];
    NSInteger count = self.viewControllers.count;
    CGFloat w = self.tabBar.bounds.size.width / count - 1;
    self.composeBtn.frame = CGRectInset(self.tabBar.bounds, 2 * w, 0);
    [self.composeBtn addTarget:self action:@selector(composeStatus) forControlEvents:UIControlEventTouchUpInside];
}

/**
 初始化tabBarController的自控制器的数组
 */
- (void)setupViewControllers {
    // 获取沙盒路径
    NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *jsonPath = [docDir stringByAppendingPathComponent:@"main.json"];
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    
    if (data == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"main.json" ofType:nil];
        data = [NSData dataWithContentsOfFile:path];
    }
    
    NSArray *dictArray = (NSArray *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers  error:nil];
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSDictionary *dict in dictArray) {
        TWSNavigationController *vc = (TWSNavigationController *)[self setupNaviControllerWithDict:dict];
        [mArray addObject:vc];
    }
    self.viewControllers = mArray;
}

/**
创建导航控制器
 */
- (UIViewController *)setupNaviControllerWithDict: (NSDictionary *)dict {
    NSString *clsName = dict[@"clsName"];
    NSString *imageName = dict[@"imageName"];
    TWSBaseViewController *vc = (TWSBaseViewController *)[[NSClassFromString(clsName) alloc] init];
    TWSNavigationController *naviVc = [[TWSNavigationController alloc] initWithRootViewController:vc];
    vc.title = dict[@"title"];
    NSLog(@"%@", dict[@"visitorDict"]);
    if ((NSDictionary *)dict[@"visitorDict"] == nil) {
        return [[UIViewController alloc] init];
    }
    vc.visitorDictionary = dict[@"visitorDict"]; // 用于访客视图
    vc.tabBarItem.image = [[UIImage imageNamed: [NSString stringWithFormat:@"tabbar_%@", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@_selected", imageName]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} forState:UIControlStateSelected];
    return naviVc;
}
#pragma mark - ControlEvent
- (void)composeStatus {
    NSLog(@"撰写微博");
    TWSComposeViewController *vc = [[TWSComposeViewController alloc] init];
    TWSNavigationController *naviVc = [[TWSNavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:naviVc animated:YES completion:nil];
    
    
}

#pragma mark - lazy load
- (UIButton *)composeBtn {
    if (_composeBtn == nil) {
        _composeBtn = [UIButton cz_imageButton:@"tabbar_compose_icon_add" backgroundImageName:@"tabbar_compose_button"];
    }
    return _composeBtn;
}

@end
